﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace NinjaToolbox.SubComponentModule
{
    [CustomEditor(typeof(SubComponentMonoContainer))]
    public class SubComponentMonoContainerInspector : Editor
    {
        bool mIsRecordUndo;
        SubComponentContainerGUIDrawer mSubComponentContainerGUIDrawer;


        public override void OnInspectorGUI()
        {
            if (mSubComponentContainerGUIDrawer == null)
            {
                var container = base.target as SubComponentMonoContainer;
                var subComponentContainer = container.GetType()
                    .GetField("mSubComponentContainer", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance)
                    .GetValue(container) as SubComponentContainer;

                mSubComponentContainerGUIDrawer = new SubComponentContainerGUIDrawer(subComponentContainer, "SubComponentContainer");
            }

            if (!mIsRecordUndo)
                Undo.RecordObject(target, "SubComponentMonoContainerInspector");

            EditorGUILayout.Space();

            serializedObject.Update();

            EditorGUI.BeginChangeCheck();
            mSubComponentContainerGUIDrawer.DrawGUI();
            if (EditorGUI.EndChangeCheck())
            {
                mIsRecordUndo = false;
                serializedObject.ApplyModifiedProperties();
                GUI.changed = true;
            }
        }
    }
}
